﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace HYWJ.jxgl
{
    public partial class TJSTForm : Office2007Form
    {
        public TJSTForm()
        {
            InitializeComponent();
        }

        SqlCommand command = null;
        SqlDataReader dataReader = null;
        string sql = "";
        int subjectID = 0;

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TJSTForm_Load(object sender, EventArgs e)
        {

        }
        
        //添加按钮
        private void button1_Click(object sender, EventArgs e)
        {
            string Answer = "";
            if (txtQuestion.Text=="")
            {
                MessageBox.Show("请输入问题","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            else if (txtResultA.Text=="")
            {
                MessageBox.Show("请输入答案A", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (txtResultB.Text=="")
            {
                MessageBox.Show("请输入答案B", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (txtResultC.Text == "")
            {
                MessageBox.Show("请输入答案C", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (txtResultD.Text == "")
            {
                MessageBox.Show("请输入答案D", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            else
            {
                if (rdoResult1.Checked==true)
                {
                    Answer = "A";
                }
                else if (rdoResult2.Checked==true)
                {
                    Answer = "B";
                }
                else if (rdoResult3.Checked==true)
                {
                    Answer = "C";
                }
                else if (rdoResult4.Checked == true)
                {
                    Answer = "D";
                }
                else
                {
                    return;
                }
                //取科目的ID值赋给变量subjectID
                try
                {
                    DataBase.OpenDataBase();
                    sql = string.Format("select subjectID from subject where subjectName='{0}'", cboSubject.Text);
                    command = new SqlCommand(sql, DataBase.connection);
                    /////
                    dataReader = command.ExecuteReader();
                    if (dataReader.Read())
                    {
                        subjectID = (int)dataReader[0];
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    dataReader.Close();
                    
                }
                //执行SQL插入语句,将数据保存到数据库
                try
                {
                    sql = string.Format("Insert into Question (Question,OptionA,OptionB,OptionC,OptionD,Answer,subjectId) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", txtQuestion.Text, txtResultA.Text, txtResultB.Text, txtResultC.Text, txtResultD.Text, Answer,subjectID);
                    command = new SqlCommand(sql, DataBase.connection);
                    int n = (int)command.ExecuteNonQuery();
                    if (n < 1)
                    {
                        MessageBox.Show("新增失败", "结果提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("新增成功", "结果提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                txtQuestion.Text = "";
                txtResultA.Clear();
                txtResultB.Clear();
                txtResultC.Clear();
                txtResultD.Clear();
                
            }

        }

        private void TJSTForm_Load_1(object sender, EventArgs e)
        {

        }
    }
}
